<?php
namespace app\appclass\controller;
use app\AdminRest;

use app\appclass\model\CardList;
use app\appclass\model\CardTime;
use app\appclass\model\CardUser;
use app\appclass\model\ClassCate;
use app\appclass\model\ClassDate;
use app\appclass\model\ClassLable;
use app\appclass\model\ClassList;
use app\appclass\model\ClassRecord;
use app\appclass\model\ClassRoom;
use app\shop\model\IndexCompany;
use think\App;




class AdminCard extends AdminRest
{

    protected $model;

    protected $cate_model;

    protected $lable_model;

    protected $room_model;

    protected $date_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new CardList();

        $this->cate_model  = new ClassCate();

        $this->lable_model = new ClassLable();

        $this->room_model  = new ClassRoom();

        $this->date_model  = new ClassDate();

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 17:50
     * @功能说明:会员卡列表
     */

    public function cardList(){

        $input = $this->_param;

        $dis[] = ['uniacid','=',$this->_uniacid];

        if(!empty($input['store_id'])){

            $dis[] = ['store_id','=',$input['store_id']];

        }

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $this->model->dataList($dis,$input['limit']);

        if(!empty($data['data'])){

            $company_model = new IndexCompany();

            foreach ($data['data'] as &$v){

                $v['store_name'] = $company_model->where(['id'=>$v['store_id']])->value('name');
            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:01
     * @功能说明:添加会员卡
     */

    public function cardAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

       // $this->model->check($input);

        $res = $this->model->adminDataAdd($input);

        return $this->success($res);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:18
     * @功能说明:会员卡详情
     */
    public function cardInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $card_time_model = new CardTime();

        $week['week'] = $card_time_model->where(['card_id'=>$data['id']])->group('week')->column('week');

        $week['time'] = $card_time_model->where(['card_id'=>$data['id']])->group('start_time')->select()->toArray();

        $week['week'] = !empty($week['week'])?array_values($week['week']):[];

        $data['card_time'] = $week;

        if(!empty($data['class_card'])){

            $class_model = new ClassList();

            foreach ($data ['class_card'] as &$v){

                $v['class_name'] = $class_model->where(['id'=>$v['class_id']])->value('title');

                $v['cate_id']    = $class_model->where(['id'=>$v['class_id']])->value('cate_id');

            }
        }

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-11 18:19
     * @功能说明:编辑会员卡
     */
    public function cardUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        //$this->model->check($input);

        $res = $this->model->adminDataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 16:38
     * @功能说明:修改会员卡状态
     */
    public function cardStatusUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $input['uniacid'] = $this->_uniacid;

        if(isset($input['status'])&&$input['status']==-1){

            $card_model = new CardUser();

            $where[] = ['card_id','=',$input['id']];

            $where[] = ['is_cancel','=',0];

            $where[] = ['end_time','>',time()];

            $find  = $card_model->dataInfo($where);

            if(!empty($find)){

                $this->errorMsg('该卡正在被使用'.$find['id']);
            }

        }

        $res = $this->model->dataUpdate($dis,$input);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-13 17:48
     * @功能说明:会员卡下拉框
     */
    public function cardSelect(){

        $input = $this->_param;

        $dis = [

            'store_id' => $input['store_id'],

            'status'   => 1,

            'uniacid'  => $this->_uniacid
        ];

        $type = [0,1,2];

        foreach ($type as $k=> $value){

            $dis['type'] = $value;

            $data[$k]['children'] = $this->model->where($dis)->order('top desc,id desc')->select()->toArray();

            $data[$k]['id'] = $value;

            if($value==0){

                $data[$k]['title'] = '期限卡';

            }elseif ($value==1){

                $data[$k]['title'] = '次数卡';

            }elseif ($value==2){

                $data[$k]['title'] = '储值卡';

            }

        }

        return $this->success($data);

    }

    /**
     * @author chenniang
     * @DataTime: 2021-05-20 10:14
     * @功能说明:会员预约列表
     */
    public function cardAppRecord(){

        $input = $this->_param;

        $dis = [

            'user_id' => $input['user_id'],

            'card_id' => $input['card_id']
        ];

        $record_model = new ClassRecord();

        $data = $record_model->dataList($dis);

        return $this->success($data);

    }











}
